package com.example.blog.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.mapper.FollowMapper;
import com.example.blog.mapper.UserMapper;
import com.example.blog.pojo.Follow;
import com.example.blog.pojo.Result;
import com.example.blog.pojo.User;
import com.example.blog.service.FollowService;
import com.example.blog.util.TokenUtil.decryptToken;
import com.example.blog.util.TokenUtil.spiltUtils;
import com.example.blog.util.UserUtils.UserUtils;
import com.xiaoleilu.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Resource
    private FollowMapper followMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserUtils userIdUtil;

    /***
     * 关注模块
     * @param followuserid
     * @param isFollow
     * @param token
     * @return
     */
    @Override
    public Result<T> follow(Long followuserid, String isFollow, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        //获取登录用户id
        Integer userId = userIdUtil.getUserId(userName);
        //判断是否关注
        if (Objects.equals(isFollow, "ture")) {
            //取关，删除关注数据
            QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
            QueryWrapper<Follow> eq = followQueryWrapper.eq("userid", userId)
                    .eq("followuserid", followuserid);
            List<Follow> followList = followMapper.selectList(eq);
            String followJson = JSONUtil.toJsonStr(followList);
            Follow follow = JSONObject.parseObject(followJson, Follow.class);
            Long id = follow.getId();
            followMapper.deleteById(id);
            return Result.success("你已经取消关注！");

        }
        //关注，新增关注数据
        Follow follow = new Follow();
        follow.setUserid(Long.valueOf(userId));
        follow.setFollowuserid(followuserid);
        followMapper.insert(follow);
        return Result.success("关注成功！");
    }

    /***
     * 判断是否关注
     */
    @Override
    public Result<T> isFollow(Long followuserid, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        //获取登录用户id
        Integer userId = userIdUtil.getUserId(userName);
        //查询是否关注
        Long count = query().eq("userid", userId).eq("followuserid", followuserid).count();
        //判断
        if (count > 0) {
            return Result.success("ture");
        }
        return Result.failed("false");
    }

    /***
     * 展示已关注用户
     * @param token
     * @return
     */
    @Override
    public Result<List<User>> getFollowUsers(String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        //获取登录用户id
        Integer userId = userIdUtil.getUserId(userName);
        //获取用户关注用户全部id
        QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
        followQueryWrapper.eq("userid", userId);
        List<Follow> followList = followMapper.selectList(followQueryWrapper);
        List<Long> followIds = new ArrayList<>();
        for (Follow follow : followList) {
            followIds.add(follow.getFollowuserid());
        }
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        if (followIds.size()==0){
            return Result.failed("你目前还没有关注用户！");
        }
        userWrapper.in("id", followIds);
        List<User> userList = userMapper.selectList(userWrapper);
        return Result.success(userList);
    }

    /***
     * 获取粉丝列表
     * @param token
     * @return
     */
    @Override
    public Result<List<User>> getFans(String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        //获取登录用户id
        Integer userId = userIdUtil.getUserId(userName);
        //获取关注当前用户的全部用户id
        QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
        followQueryWrapper.eq("followuserid", userId);
        List<Follow> followList = followMapper.selectList(followQueryWrapper);
        List<Long> userid = new ArrayList<>();
        for (Follow follow : followList) {
            userid.add(follow.getUserid());
        }
        QueryWrapper<User> fanWrapper = new QueryWrapper<>();
        if (userid.size()==0){
            return Result.failed("你目前还没有被用户关注QAQ~~！");
        }
        fanWrapper.in("id", userid);
        List<User> userList = userMapper.selectList(fanWrapper);
        return Result.success(userList);
    }

    /***
     * 获取粉丝数
     * @return
     */
    @Override
    public Result<Integer> getFansCount(String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        Integer userId = userIdUtil.getUserId(userName);
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Follow> followQueryWrapper = queryWrapper.eq("followuserid", userId);
        int size = followMapper.selectList(followQueryWrapper).size();
        return Result.success(size);
    }

}
